<?php
/**
 * Created by PhpStorm.
 * User: PGF(592472116@qq.com)
 * Date: 2016/8/22
 * Time: 10:48
 */

namespace Appapi\Controller;
use Common\Controller\HomebaseController;


class NotifyController  extends HomebaseController
{
	//--支付宝异步通知地址
	public function aliPay(){

        $order_no=$_POST['out_trade_no'];
        $total_fee=$_POST['total_amount'];
        $trade_no=$_POST['trade_no'];
        file_put_contents('./zhifubao.txt',json_encode($_POST));
       /* if($_POST['seller_id'] != '2088721119028384'){
            exit;
        }*/
        $map['status'] =0;
        $map['money'] = $total_fee;
        $map['orderno'] = $order_no;
        $order = M('users_charge')->where($map)->find();
        if($order){
            $data['status'] =1;
            $data['orderno']=$order_no;
            $data['trade_no'] = $trade_no;
            $data['addtime']  = time();
            $res = M('users_charge')->where('id='.$order['id'])->save($data);
            $coin=$order['coin'];
            if($order['coin_give']>0){
                $coin = $coin+$order['coin_give'];
            }
            if($order['effect_type'] ==2){
                $month=$order['month'];
                $nowtime=time();
                $user=M('users')->where('id='.$order['uid'])->find();
                if($user['member_validity']){
                    if($nowtime>$user['member_validity']){
                        $efftime =strtotime('+'.$month.'month');
                    }else{
                        $efftime =strtotime('+'.$month.' month',$user['member_validity']);
                    }

                }else{
                    $efftime =strtotime('+'.$month.'month');
                }
                //$efftime =strtotime('+'.$month.'month');
                $where['member_validity']=$efftime;
                $user_flag = M('users')->where('id=' . $order['uid'])->save($where);
            }else {
                $user_flag = M('users')->where('id=' . $order['uid'])->setInc('coin', $coin);
            }
            //添加三级分销
            $dis = M('distribution')->find();
            if($dis['status'] == 1) { //是否开启三级分销
                $price = $order['money'];//实际购买播豆支付的钱
                $id= $order['id'];
                $member=M('Member','jzb_');
                $ucenter_member=M('Ucenter_member','jzb_');

                $mobile  =M('users')->where('id='.$order['uid'])->getField('user_login');
                $uid =$ucenter_member->where('username='.$mobile)->getField('id');
                //一级分销
                $referrer1 = $member->where('uid=' .$uid)->getField('referrer');
                if ($referrer1) {
                    $rer1_uid = $ucenter_member->where('username=' . $referrer1)->getField('uid');
                    $one_money = $price * $dis['one_ratio']/100;
                    $re1 = M('member')->where('uid=' . $rer1_uid)->setField('amount', $one_money);
                    payrecord($rer1_uid, $id, $one_money, 5); //佣金记录
                    //二级分销
                    $referrer2 = $member->where('uid=' .$rer1_uid)->getField('referrer');
                    if($referrer2){
                        $rer2_uid = $ucenter_member->where('username=' . $referrer2)->getField('uid');
                        $two_money = $price*$dis['two_ratio']/100;
                        $re2 = $member->where('uid=' . $rer2_uid)->setField('amount', $two_money);
                        payrecord($rer2_uid, $id, $two_money, 5); //佣金记录

                        //三级分销
                        $referrer3 = $member->where('uid=' .$rer2_uid)->getField('referrer');
                        if($referrer3) {
                            $rer3_uid =$ucenter_member->where('username=' . $referrer2)->getField('uid');
                            $three_money = $price * $dis['three_ratio'] / 100;
                            $re2 =$member->where('uid=' . $rer3_uid)->setField('amount', $three_money);
                            payrecord($rer3_uid, $id, $three_money, 5); //佣金记录
                        }  //三级分销if结束
                    }   //二级分销if结束
                }  //一级分销if结束
            }
            echo 'SUCCESS';
        }

    }

    //微信支付回调地址
    public function WXPay()
    {
        $xmldata = file_get_contents("php://input");
        file_put_contents('./xmldata.txt', $xmldata);
        libxml_disable_entity_loader(true);
        $data = (array)simplexml_load_string($xmldata, 'SimpleXMLElement', LIBXML_NOCDATA);
        file_put_contents('./wx.txt', json_encode($data));
        if ($data) {
            $out_trade_no = $data['out_trade_no'];//订单号
            $money = $data['total_fee'] / 100;  //充值金额
            $map['status'] =0;;
            $map['money'] = $money;
            $map['orderno'] = $out_trade_no;
                $order = M('users_charge')->where($map)->find();
            if($order){
                $data['status'] =1;
                $data['orderno']=$out_trade_no;
               // $data['trade_no'] = $trade_no;
                $data['addtime']  = time();
                $res = M('users_charge')->where('id='.$order['id'])->save($data);
                $coin=$order['coin'];
                if($order['coin_give']>0){
                    $coin = $coin+$order['coin_give'];
                }
                if($order['effect_type'] ==2){
                    $month=$order['month'];
                    $nowtime=time();
                    $user=M('users')->where('id='.$order['uid'])->find();
                    if($user['member_validity']){
                        if($nowtime>$user['member_validity']){
                            $efftime =strtotime('+'.$month.'month');
                        }else{
                            $efftime =strtotime('+'.$month.' month',$user['member_validity']);
                        }

                    }else{
                        $efftime =strtotime('+'.$month.'month');
                    }
                    //$efftime =strtotime('+'.$month.'month');
                    $where['member_validity']=$efftime;
                    $user_flag = M('users')->where('id=' . $order['uid'])->save($where);
                }else {
                    $user_flag = M('users')->where('id=' . $order['uid'])->setInc('coin', $coin);
                }

                //添加三级分销
                $dis = M('distribution')->find();
                if($dis['status'] == 1) { //是否开启三级分销
                    $price = $order['money'];//实际购买播豆支付的钱
                    $id= $order['id'];
                    $member=M('Member','jzb_');
                    $ucenter_member=M('Ucenter_member','jzb_');

                    $mobile  =M('users')->where('id='.$order['uid'])->getField('user_login');
                    $uid =$ucenter_member->where('username='.$mobile)->getField('id');
                    //一级分销
                    $referrer1 = $member->where('uid=' .$uid)->getField('referrer');
                    if ($referrer1) {
                        $rer1_uid = $ucenter_member->where('username=' . $referrer1)->getField('uid');
                        $one_money = $price * $dis['one_ratio']/100;
                        $re1 = M('member')->where('uid=' . $rer1_uid)->setField('amount', $one_money);
                        payrecord($rer1_uid, $id, $one_money, 5); //佣金记录
                        //二级分销
                        $referrer2 = $member->where('uid=' .$rer1_uid)->getField('referrer');
                        if($referrer2){
                            $rer2_uid = $ucenter_member->where('username=' . $referrer2)->getField('uid');
                            $two_money = $price*$dis['two_ratio']/100;
                            $re2 = $member->where('uid=' . $rer2_uid)->setField('amount', $two_money);
                            payrecord($rer2_uid, $id, $two_money, 5); //佣金记录

                            //三级分销
                            $referrer3 = $member->where('uid=' .$rer2_uid)->getField('referrer');
                            if($referrer3) {
                                $rer3_uid =$ucenter_member->where('username=' . $referrer2)->getField('uid');
                                $three_money = $price * $dis['three_ratio'] / 100;
                                $re2 =$member->where('uid=' . $rer3_uid)->setField('amount', $three_money);
                                payrecord($rer3_uid, $id, $three_money, 5); //佣金记录
                            }  //三级分销if结束
                        }   //二级分销if结束
                    }  //一级分销if结束
                }
                echo 'SUCCESS';
            }

            $data['return_code'] = 'SUCCESS';
            $data['return_msg'] = 'OK';
            $xml = data_to_xml2( $data );
            echo $xml;
            die();
        }

    }



}